package com.ybxy;

import java.util.Comparator;
import java.util.TreeMap;

public class TreeMapPractice {
    public static void main(String[] args) {

        test1();

        System.out.println("-------------------------------------------");
        test2();
    }

    /**
     * 需求：创建一个TreeMap集合，键是学生对象，值是籍贯
     * 1.创建TreeMap集合
     * 2.创建学生对象
     * 3.将学生和籍贯添加到TreeMap集合
     * 4.遍历TreeMap集合
     */
    private static void test1() {
        TreeMap<Student,String> treeMap = new TreeMap<>();
        treeMap.put(new Student("小王1", 18), "宜宾");
        treeMap.put(new Student("小王2", 19), "上海");
        treeMap.put(new Student("小王3", 20), "北京");
        treeMap.put(new Student("小王4", 21), "上海");
        treeMap.put(new Student("小王5", 22), "上海");

        treeMap.forEach((k,v)-> System.out.println(k.getName()+" " +k.getAge()+"岁" + ":" + v));
    }


    public static  void test2(){

        TreeMap<Student,String> treeMap1 = new TreeMap<>(new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                return o2.getAge()-o1.getAge();
            }
        });


        treeMap1.put(new Student("小王1", 18), "宜宾");
        treeMap1.put(new Student("小王2", 19), "上海");
        treeMap1.put(new Student("小王3", 20), "北京");
        treeMap1.put(new Student("小王4", 21), "上海");
        treeMap1.put(new Student("小王5", 22), "上海");

        treeMap1.forEach((k,v)-> System.out.println(k.getName()+" " +k.getAge()+"岁" + ":" + v));

    }
}
